package com.banksoft.admin.controller;

import com.banksoft.admin.bean.Status;
import com.banksoft.admin.dao.NoticeDao;
import com.banksoft.admin.entity.Admin;
import com.banksoft.admin.entity.Notice;
import com.banksoft.admin.service.BaseService;
import com.banksoft.common.BaseController;
import com.banksoft.common.PageInfo;
import com.banksoft.util.StringUtil;
import com.banksoft.util.UUIDFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/5.
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {
    @Autowired
    BaseService baseService;

    @RequestMapping("/list")
    public String list(){
        return "notice/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String title,String status,PageInfo pageInfo){

        Map<String,Object> map = new HashMap<>();
        if(StringUtil.isNotNull(title))
            map.put("title",title);
        if(StringUtil.isNotNull(status))
            map.put("status",status);
        pageInfo.setParams(map);

        pageInfo = this.page(NoticeDao.class,pageInfo);

        return this.getPageResult(pageInfo);
    }


    @RequestMapping("/beforeEdit")
    public String beforeEdit(String id,Model model){
        if(StringUtil.isNotNull(id)){
            Notice notice = this.baseService.findById(NoticeDao.class,id);
            model.addAttribute("data",notice);
        }
        return "notice/edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(Notice notice,HttpServletRequest request){

        try {
            if (StringUtil.isNotNull(notice.getId())) {//编辑

                this.baseService.update(NoticeDao.class, notice);
                this.saveLog("编辑公告" + notice.getTitle(), request);
            } else {//新增
                notice.setId(UUIDFactory.random());
                notice.setCreateTime(new Date().getTime());
                notice.setStatus(Status.NORMAL.name());
                Admin admin = (Admin) getCurrentUser(request);
                if(admin!=null)
                    notice.setPubMan(admin.getLoginName());

                this.baseService.insert(NoticeDao.class, notice);
                this.saveLog("新增公告：" + notice.getTitle(), request);

            }
            return this.FmtResult(true,"保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,"保存失败:"+e.getMessage(),null);
        }

    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Object updateStatus(String id,String status,HttpServletRequest request){
        try{
            Notice notice = this.baseService.findById(NoticeDao.class,id);
            notice.setStatus(status);
            this.baseService.update(NoticeDao.class,notice);
            if(status.equals(Status.NORMAL.name())){
                this.saveLog("解冻公告："+notice.getTitle(),request);
            }else{
                this.saveLog("冻结公告："+notice.getTitle(),request);
            }
            return this.FmtResult(true,"操作成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,"操作失败"+e.getMessage(),null);
        }
    }

}
